package ConfigFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import javax.swing.JPanel;

import model.general.Util;
import model.objects.Boat;
import model.objects.Hook;
import model.objects.Lighting;

public class Loads {
	private static ConfigFile savefile;
	private JPanel pnl;

	public Loads(JPanel pnl) {
		this.pnl = pnl;
		this.savefile = loadConfigFile();
	}

	public Loads() {
	}

	public ConfigFile getConfigFile() {
		return loadConfigFile();
	}

	public static ScoreFile loadScoreFile() {
		File file = new File(Util.HIGHT_SCORE_FILE);
		if (file.exists()) {
			try {
				ObjectInputStream ois = new ObjectInputStream(
						new FileInputStream(file));
				Object obj = ois.readObject();
				if (obj instanceof ScoreFile) {
					return (ScoreFile) obj;
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;

	}

	public static ConfigFile loadConfigFile() {
		File file = new File(Util.CONFIG_FILE);
		System.out.println("file exit " + file.exists());
		if (file.exists()) {
			try {
				ObjectInputStream ois = new ObjectInputStream(
						new FileInputStream(file));
				Object obj = ois.readObject();
				ois.close();
				if (obj instanceof ConfigFile) {
					return (ConfigFile) obj;
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}

	public boolean exitsData() {
		if (savefile != null) {
			return savefile.idExitsData();
		} else {
			return false;
		}
	}

	public Boat getBoat() {
		String b1 = savefile.getElementProperties(Util.ID_BOAT_IMG_LEFT);
		String b2 = savefile.getElementProperties(Util.ID_BOAT_IMG_RIGHT);
		if ((b1 != null) && (b2 != null)) {
			Util.BOAT_IMG_LEFT = b1;
			Util.BOAT_IMG_RIGHT = b2;
		}
		Boat boat = new Boat(savefile.getLocation_boat(), Util.BOAT_IMG_LEFT,
				Util.BOAT_IMG_RIGHT, pnl);
		boat.setLife(getLife());
		boat.setScore(getScore());
		System.out.println(getLevel()+" :level");
		System.out.println(getLife()+" :life");
		System.out.println(getScore()+" :score");
		return boat;
	}

	public Hook getHook() {

		String h1 = savefile.getElementProperties(Util.ID_HOOK_IMG_LEFT);
		String h2 = savefile.getElementProperties(Util.ID_HOOK_IMG_RIGHT);
		if ((h1 != null) && (h2 != null)) {
			Util.HOOK_IMG_LEFT = h1;
			Util.HOOK_IMG_RIGHT = h2;
		}
		Hook hook = new Hook(savefile.getLocation_hook(), Util.HOOK_IMG_LEFT,
				Util.HOOK_IMG_RIGHT, pnl);
		return hook;
	}

	public Lighting getLight() {

		String l1 = savefile.getElementProperties(Util.ID_CLOUD_IMG);
		String l2 = savefile.getElementProperties(Util.ID_LIGHT_IMG);
		if ((l1 != null) && (l2 != null)) {
			Util.CLOUND_IMG = l1;
			Util.LIGHTING_IMG = l2;
		}
		Lighting lighting = new Lighting(savefile.getLocation_light(),
				Util.LIGHTING_IMG, Util.CLOUND_IMG, pnl, getBoat());
		return lighting;
	}

	public int getScore() {
		return savefile.getScore();
	}

	public int getLife() {
		return savefile.getLife();
	}

	public int getLevel() {
		return savefile.getLevel();
	}
}
